package com.moxiao.yfjh.module.blind.service.equity;

import com.moxiao.yfjh.module.blind.controller.admin.equity.vo.MemberEquityCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.equity.vo.MemberEquityExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.equity.vo.MemberEquityPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.equity.vo.MemberEquityUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.equity.MemberEquityConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.equity.MemberEquityDO;
import com.moxiao.yfjh.module.blind.dal.mysql.equity.MemberEquityMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.equity.vo.*;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲会员权益 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MemberEquityServiceImpl implements MemberEquityService {

    @Resource
    private MemberEquityMapper memberEquityMapper;

    @Override
    public Long createMemberEquity(MemberEquityCreateReqVO createReqVO) {
        // 插入
        MemberEquityDO memberEquity = MemberEquityConvert.INSTANCE.convert(createReqVO);
        memberEquityMapper.insert(memberEquity);
        // 返回
        return memberEquity.getId();
    }

    @Override
    public void updateMemberEquity(MemberEquityUpdateReqVO updateReqVO) {
        // 校验存在
        validateMemberEquityExists(updateReqVO.getId());
        // 更新
        MemberEquityDO updateObj = MemberEquityConvert.INSTANCE.convert(updateReqVO);
        memberEquityMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberEquity(Long id) {
        // 校验存在
        validateMemberEquityExists(id);
        // 删除
        memberEquityMapper.deleteById(id);
    }

    private void validateMemberEquityExists(Long id) {
        if (memberEquityMapper.selectById(id) == null) {
            throw exception(MEMBER_EQUITY_NOT_EXISTS);
        }
    }

    @Override
    public MemberEquityDO getMemberEquity(Long id) {
        return memberEquityMapper.selectById(id);
    }

    @Override
    public List<MemberEquityDO> getMemberEquityList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberEquityMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MemberEquityDO> getMemberEquityPage(MemberEquityPageReqVO pageReqVO) {
        return memberEquityMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MemberEquityDO> getMemberEquityList(MemberEquityExportReqVO exportReqVO) {
        return memberEquityMapper.selectList(exportReqVO);
    }

    @Override
    public Long memberEquityNum(Long userId,String equityName,Date firstDayOfMonth,Date lastDayOfMonth) {
        return memberEquityMapper.getMemberEquity(userId,equityName,firstDayOfMonth,lastDayOfMonth);
    }

}
